package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.bean.ClassifyGift;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzw on 2015/12/5.
 */
public class GiftRecyclerViewAdapter extends RecyclerView.Adapter<GiftRecyclerViewAdapter.ViewHolder> {
    private LayoutInflater mInflater;

    private List<ClassifyGift.DataEntity.CategoriesEntity> mCategories;
    private ClassifyGift mCfg;
    private static Context mContext;
    private GiftGridRecycAdapter mGgrAdapter;



    public GiftRecyclerViewAdapter(Context context, Object datas) {
        mInflater = LayoutInflater.from(context);

        mContext = context;

        mCategories = new ArrayList<>();
        if (datas != null) {
            mCfg = (ClassifyGift) datas;
            mCategories = mCfg.getData().getCategories();

        }
    }


    @Override
    public GiftRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_gift_recyc,parent,false));
    }

    @Override
    public void onBindViewHolder(GiftRecyclerViewAdapter.ViewHolder holder, int position) {
        holder.tvTitle.setText(mCategories.get(position).getName());


        //设置内部RecyclerView 的 Grid类型的布局，并适配
        FullyGridLayoutManager gridLayoutManager = new FullyGridLayoutManager(mContext, 3);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        gridLayoutManager.setSmoothScrollbarEnabled(true);
        holder.rvContent.setLayoutManager(gridLayoutManager);

        mGgrAdapter = new GiftGridRecycAdapter(mContext,mCategories.get(position).getSubcategories());
        holder.rvContent.setAdapter(mGgrAdapter);


    }

    @Override
    public int getItemCount() {
        return mCategories == null? 0:mCategories.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        RecyclerView rvContent;


        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_gift_recyc_text);
            rvContent = (RecyclerView) itemView.findViewById(R.id.recyc_gift_grid);

        }
    }

}
